home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / me_cd25.zip / MUTT2.ZIP / ISEARCH.MUT < prev    next >
Lisp/Scheme  |  1992-11-09  |  5KB  |  162 lines

  1.   ;; isearch.mut: Incremental searching.
  2.  
  3.   ;;   Searching as you type - shows you where the string you have typed so
  4.   ;; far would be found.
  5.   
  6.   ;; Keys:
  7.   ;;  ^H (Backspace):  Remove the last character from the search pattern.
  8.   ;;     Don't move.
  9.   ;;  ^S:  Search forward for existing pattern.  If haven't entered a
  10.   ;;     pattern, the old pattern is used.
  11.   ;;  ^R:  Search reverse for pattern.
  12.   ;;  ^Q:  Quote the next character into the search pattern.  Useful for
  13.   ;;     searching for control characters.
  14.   ;;  ^W:  Add the rest of the word to the search pattern.
  15.   ;;  ^G:  Exit search.
  16.   ;;  ^M (Enter): Drop to non incremental search if haven't done anything
  17.   ;;     yet.
  18.   ;;  Control characters and combo keys (eg cursor motion, C-X=, etc)
  19.   ;;    terminate the search.  The key is then executed.  For example,
  20.   ;;    searching for text and then pressing C-P will cause the cursor to
  21.   ;;    move to the line above the matched text.
  22.  
  23.   ;;   If you want to call isearch from a pgm, prime and start searching,
  24.   ;; just pass in a search string.  eg (isearch TRUE "foobar") will start a
  25.   ;; incremental search at the point and search for foobar.
  26.  
  27.   ;; C Durland    Public Domain
  28.  
  29. (defun
  30.   isearch-forward { (isearch TRUE)  }    ;; Normally bound to C-S
  31.   isearch-reverse { (isearch FALSE) }    ;; Normally bound to C-R
  32.   MAIN
  33.   {
  34.     (bind-to-key "isearch-forward" "C-s")
  35.     (bind-to-key "isearch-forward" 'C-\')
  36.     (bind-to-key "isearch-reverse" "C-r")
  37.   }
  38. )
  39.  
  40.  
  41. (include me2.h)
  42. (string oldpat)
  43.  
  44. (defun isearch (bool forward-search)  ;; [(string pattern)]
  45. {
  46.   (string ipat hoho tmp direction)
  47.   (int sc len)
  48.   (bool firsttime forward)
  49.  
  50.   (direction (if (forward forward-search) "forward" "reverse"))
  51.   (ipat (hoho ""))
  52.  
  53.     ;; check to see pattern was passed in
  54.   (if (== 2 (nargs)) { (ipat (arg 1))(len 0)(goto ack) })
  55.  
  56.   (firsttime TRUE)
  57.   (while TRUE
  58.   {
  59.     (msg "I-search-" direction " [" ipat "] " hoho ' (^S ^R ^W ^Q)')
  60.     (if (not (key-waiting)) (update))
  61.     (hoho "")
  62.     (switch (sc (get-key))
  63.       0x148 (ipat (extract-elements ipat 0 -1))        ;; backspace
  64.       0x153                ;; ^S: forward search, same pattern
  65.       {
  66.       (label I-hate-XON/XOFF)
  67.     (forward TRUE)(direction "forward")
  68.     (if firsttime
  69.     {
  70.       (firsttime FALSE)
  71.       (if (== oldpat "")(continue))
  72.       (ipat oldpat)
  73.     })
  74.     (if (not (search-forward ipat))(hoho "Not found."))
  75.       }
  76.       0x15C (goto I-hate-XON/XOFF)    ;; ^\: same as ^S
  77.       0x152                ;; ^R: reverse the search
  78.       {
  79.     (forward FALSE)(direction "reverse")
  80.     (if firsttime
  81.       {
  82.         (firsttime FALSE)
  83.         (if (== oldpat "")(continue))
  84.         (ipat oldpat)
  85.         (if (search-reverse ipat)
  86.           (search-forward ipat)        ;; put dot at end of pattern
  87.           (hoho "Not found.")
  88.         )
  89.       }
  90.       {
  91.         (arg-prefix (length-of ipat))(previous-character)
  92.         (if (search-reverse ipat)
  93.           (search-forward ipat)        ;; put dot at end of pattern
  94.           {
  95.         (arg-prefix (length-of ipat))(next-character)
  96.         (hoho "Not found.")
  97.           }
  98.         )
  99.       }
  100.     )
  101.       }
  102.       0x151                ;; ^Q: quote next character
  103.       {
  104.     (len (length-of ipat))
  105.     (ipat (concat ipat (getchar)))
  106.     (goto ack)
  107.       }
  108.       0x157                ;; ^W: grab word
  109.       {
  110.     (if (looking-at '\w+')
  111.     {
  112.       (len (length-of ipat))
  113.       (ipat (concat ipat (get-matched '&')))
  114.       (goto ack)    ; we know the search will succeed
  115.     })
  116.     (hoho "Not looking at a word.")
  117.       }
  118.       0x14D            ;; ^M: drop down into non incremental search
  119.       {
  120.     (if firsttime
  121.     {
  122. ;;;;!!! use a default (if available)
  123.       (ask-user)(ipat (ask "Search for: "))
  124.       (if forward (search-forward ipat) (search-reverse ipat))
  125.       (goto done-searching)
  126.     })
  127.       }
  128.       0x147                ;; ^G: quit
  129.       {
  130.     (msg "Done.")
  131.       (label done-searching)
  132.     (if (!= "" ipat) (oldpat ipat))    ; save search pattern (if interesting).
  133.     (done)
  134.       }
  135.       default
  136.       {
  137.     (if (> sc 0xFF) { (msg "")(exe-key sc)(goto done-searching) })
  138.     (len (length-of ipat))
  139. ;    (ipat (concat ipat (chr$ sc tmp)))
  140.     (ipat (concat ipat (convert-to CHARACTER sc)))
  141.       (label ack)
  142.     (firsttime FALSE)
  143.     (if forward
  144.       {
  145.         (arg-prefix len)(previous-character)  ;; move to start of pattern
  146.         (if (not (search-forward ipat))
  147.         {
  148.           (arg-prefix len)(next-character)
  149.         (label gack)
  150.           (ipat (extract-elements ipat 0 -1))(hoho "Not found.")
  151.         })
  152.       }
  153.       {
  154.         (if (search-reverse ipat)
  155.           (search-forward ipat)
  156.           (goto gack)
  157.         )
  158.       })
  159.       })
  160.   })
  161. })
  162.